<?php

/**
 *
 * @author iegss
 *        
 */
class mobileapi_rpc_keywords {

    /**
     * 构造函数
     * 
     * @param app $app
     */
    function __construct($app) {
        $this->app = $app;
    }

    /**
     * 获取关键字列表
     * 
     * @return array
     */
    function get_all_list() {
        $db = kernel::database();
        $sql = "select kw_name  FROM `sdb_mobileapi_keywords` order by ordernum asc;";
        $keywords = $db->select($sql);

        $ret['items'] = $keywords ? $keywords : array();
        $ret['show_num'] = count($keywords) > 10 ? 10 : count($keywords);

        return $ret;
    }

    /**
     * 获取联想关键字
     * 
     * @return array
     */
    function get_guess_list() {
        $filter = array('kw_name|has' => $_POST['key']);
        if(!isset($_POST['key']) || empty($_POST['key'])){
            $ret['items'] = array();
            $ret['show_num'] = count($keywords) > 10 ? 10 : count($keywords);
            return $ret;
        }
        // 获取联想关键词信息
        $keywords = $this->app->model('keywords')->getList('kw_name', $filter); // todo只取10条信息，增加order条件，ordernum、kw_hits
        $ret['items'] = $keywords ? $keywords : array();
        $ret['show_num'] = count($keywords) > 10 ? 10 : count($keywords);

        return $ret;
    }

    public function associate() {
        $words = $_POST['words'];
        $searchrule = searchrule_search::instance('search_associate');
        if ($searchrule && !empty($words)) {
            $result = $searchrule->get_woreds($words);
            return $result;
        } else {
            return array();
        }
    }

}
